﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Google.GData.Extensions.MediaRss;
using Google.GData.YouTube;
using Google.GData.Extensions.Location;
using Google.GData.Client;
using Google.YouTube;

public partial class testVideo : System.Web.UI.Page
{
    static YouTubeRequestSettings settings = new YouTubeRequestSettings("emxp APP",
                           "AI39si7il-bxtwZqwPvbYYm_NezKJ1LjY0XCNuky8sN5ZQT5hr-mXFF6k5hhMVNvtgxo4oS9MPxEDFDHHqZ18E3g45qbwe8n4Q",
                           "leotran48@gmail.com", "1478965147953"
                           );
    YouTubeRequest request = new YouTubeRequest(settings);
    CommonPage cm6 = new CommonPage();
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadData();
        if (Request.QueryString["id"] != null)
        {
            Response.Write(Request.QueryString["id"]);
            
        }
        //ShowAllVideo();
    }
    void LoadData()
    {
        ///////
        Video newVideo = new Video();
        newVideo.Title = "My movie_"+DateTime.Today.ToLongDateString();
        newVideo.Tags.Add(new MediaCategory("Autos", YouTubeNameTable.CategorySchema));
        newVideo.Keywords = "viec lam gap, funny,hai huoc";
        newVideo.Description = "viec lam gap , ki la";
        newVideo.YouTubeEntry.Private = false;
        newVideo.Tags.Add(new MediaCategory("mydevtag, anotherdevtag",
          YouTubeNameTable.DeveloperTagSchema));

        newVideo.YouTubeEntry.Location = new GeoRssWhere(37, -122);
        FormUploadToken token = request.CreateFormUploadToken(newVideo);
        String url = token.Url;
        String redirect = Request.Url.AbsoluteUri;
        url += "?nexturl=" + HttpUtility.UrlEncode(redirect);
        Response.Write("<form action='" + url + "' method='post' enctype='multipart/form-data'>");
        Response.Write("<input type='file' name='file'/>");
        Response.Write("<input type='hidden' name='token' value='" + token.Token + "'/>");
        Response.Write("<input type='submit' value='Upload'/>");
       //Response.Write(cm6.Customer.idAcc);
        Response.Write("</form>");
        
        string str =CheckVideo(newVideo);
        Response.Write(str);
        
    }
    String CheckVideo(Video video)
    {
        string kq="";
        if (video.IsDraft)
        {
            Response.Write("Video is not live.");
            string stateName = video.Status.Name;
            if (stateName == "processing")
            {
                kq = ("Video is still being processed.");
                return kq;
            }
            else if (stateName == "rejected")
            {
                kq = ("Video has been rejected because: ");
                kq += (video.Status.Value);
                kq += ("For help visit: ");
                kq += (video.Status.Help);
                return kq;
            }
            else if (stateName == "failed")
            {
                kq = ("Video failed uploading because:");
                kq += (video.Status.Value);
                kq += ("For help visit: ");
                kq += (video.Status.Help);
                return kq;
            }
            return kq;
        }
        else
            return kq;
    }
    void ShowAllVideo()
    {
        string feeURLs = "http://gdata.youtube.com/feeds/api/users/trieuquang198812/uploads/U8N70mWPA9U";
        Feed<Video> videofeed = request.Get<Video>(new Uri(feeURLs));
        printVideoFeed(videofeed);

    }
    void printVideoFeed(Feed<Video> v_Feed)
    {
        foreach (Video entry in v_Feed.Entries)
        {
            printVideoEntity(entry);
        }
    }
    void printVideoEntity(Video video)
    {
        Response.Write("title: " + video.Title);
        Response.Write("description: " + video.Description);
        Response.Write("keywords: " + video.Keywords);
        Response.Write("Uploads by: " + video.Uploader);
        if (video.YouTubeEntry.Location != null)
        {
            Response.Write("Latitude: " + video.YouTubeEntry.Location.Latitude);
            Response.Write("Longitude: " + video.YouTubeEntry.Location.Longitude);
        }
        if (video.Media != null && video.Media.Rating != null)
        {
            Response.Write("Restricted in" + video.Media.Rating.Country);
        }
        if (video.IsDraft)
        {
            Response.Write("Video is no live");
            string state = video.Status.Name;
            if (state == "processing")
            {
                Response.Write("Video is still being processed.");
            }
            else if (state == "rejected")
            {
                Response.Write("Video has been rejected because: ");
                Response.Write(video.Status.Value);
                Response.Write("For help visit: ");
                Response.Write(video.Status.Help);
            }
            else if (state == "failed")
            {
                Response.Write("Video failed uploading because:");
                Response.Write(video.Status.Value);
                Response.Write("For help visit: ");
                Response.Write(video.Status.Help);
            }
        }
        if (video.ReadOnly == false)
        {
            Console.WriteLine("Video is editable by the current user.");
        }

        if (video.RatingAverage != -1)
        {
            Console.WriteLine("Average rating: " + video.RatingAverage);
        }
        if (video.ViewCount != -1)
        {
            Console.WriteLine("View count: " + video.ViewCount);
        }

    }

}
